<?php
// 应用公共文件

/**
 * [p 数组]
 * @return [type] [description]
 */
function p($data, $flag = 1)
{
    header("content-type:text/html;charset=utf-8");
    if (is_bool($data)) {
        echo $data ? 'TRUE' : 'FALSE';
    } else if (is_null($data)) {
        echo 'NULL';
    } else {
        echo '<pre>';
        print_r($data);
        echo '</pre>';
    }
    if ($flag == 1) {
        die;
    }
}


/**
 * 判断是否为微信浏览器
 * @return boolean
 */
function is_weixin()
{
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }
    return false;
}

/**
 * 微信屏蔽提醒 -- 引导原生浏览器打开
 *
 * @return void
 */
function wechatBlock()
{
    $html = '<div id="mark" style=\'background: url("/static/images/mark.png"); background-color: black; background-size: 100%;\'></div>';
    if (
        (time() > strtotime(date('Y-m-d 00:00:00', time()))) &&
        (time() < strtotime(date('Y-m-d 08:00:00', time())))
    ) {
        $html = '<div id="mark" style="background: black;">由于微信浏览器限制<br/>
    无法使用微信浏览器直接下载<br/>
    请点击右上角在默认浏览器打开</div>';
    }

    $html .= '<style>
    #mark {
        width: 100%;
        height: 100%;
        position: fixed;
        opacity: 0.8;
        color: white;
        z-index: 9;
        text-align: center;
        padding-top: 200px;
        font-size: 18px;
    }
    </style>';
    echo $html;
}

/**
 * 通用html头尾
 *
 * @param [type] $body
 * @param string $title
 * @return void
 */
function commonHtml($body, $title = '系统提示', $wechatBlock = false)
{
    $html = '';
    $html .= '<!DOCTYPE html>
    <html>
    <head><title>' . $title . '</title><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="blue">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
    $html .= '<link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">';
    $html .= '<link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css"></head><body>';
    $html .= '<div class="weui-msg">';
    $html .= $body;
    $html .= '<div class="weui-msg__opr-area">
    <p class="weui-btn-area">
      <a href="javascript:;" onclick="window.location.href=\'about:blank\'; window.close();" class="weui-btn weui-btn_default">关闭页面</a>
    </p>
  </div>
  <div class="weui-msg__extra-area">
    <div class="weui-footer">
      <p class="weui-footer__links">
        <a href="https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzU2Njc4NjI0NA==&subscene=0#wechat_redirect" class="weui-footer__link">ImgHelper</a>
      </p>
      <p class="weui-footer__text">Copyright © ' . date('Y') . '</p>
    </div>
  </div>';
    $html .= '</div>';
    if ($wechatBlock) {
        if (is_weixin()) {
            wechatBlock();
        }
    }
    $html .= '</body><script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>';
    $html .= '<script src="https://cdn.bootcss.com/jquery-weui/1.2.1/js/jquery-weui.min.js"></script></html>';
    die($html);
}

/**
 * 错误提示
 *
 * @param string $message
 * @return void
 */
function error($message = '系统错误')
{
    $html = '<div class="weui-msg__icon-area"><i class="weui-icon-warn weui-icon_msg"></i></div>
    <div class="weui-msg__text-area">
      <h2 class="weui-msg__title">系统提示</h2>
      <p class="weui-msg__desc">' . $message . '</p>
    </div>';
    commonHtml($html);
}
